package com.library.web.sys_category.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.library.annotation.Auth;
import com.library.utils.ResultUtils;
import com.library.utils.ResultVo;
import com.library.web.sys_category.pojo.ListCateParm;
import com.library.web.sys_category.pojo.SysCategory;
import com.library.web.sys_category.service.SysCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/category")
public class SysCategoryController {
    @Autowired
    private SysCategoryService sysCategoryService;

    //新增
    @PostMapping
    @Auth
    public ResultVo add(@RequestBody SysCategory category) {
        boolean save = sysCategoryService.save(category);
        if (save) {
            return ResultUtils.success("新增类别成功!");
        }
        return ResultUtils.error("新增类别失败!");
    }

    //新增
    @Auth
    @PutMapping
    public ResultVo edit(@RequestBody SysCategory category) {
        boolean save = sysCategoryService.updateById(category);
        if (save) {
            return ResultUtils.success("编辑类别成功!");
        }
        return ResultUtils.error("编辑类别失败!");
    }

    //删除
    @Auth
    @DeleteMapping("/{categoryId}")
    public ResultVo delete(@PathVariable("categoryId") Long categoryId) {
        boolean remove = sysCategoryService.removeById(categoryId);
        if (remove) {
            return ResultUtils.success("删除类别成功!");
        }
        return ResultUtils.error("删除类别失败!");
    }

    //列表
    @Auth
    @GetMapping("/list")
    public ResultVo getList(ListCateParm parm) {
        IPage<SysCategory> list = sysCategoryService.getList(parm);
        return ResultUtils.success("查询类别成功", list);
    }

    //图书列表分类
    @Auth
    @GetMapping("/cateList")
    public ResultVo getCateList() {
        List<SysCategory> list = sysCategoryService.list();
        return ResultUtils.success("查询成功", list);
    }

}
